package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.context.BaseContext;
import com.example.demo.dto.OkDTO;
import com.example.demo.pojo.ElderlyBasicOk;
import com.example.demo.pojo.ElderlyService;
import com.example.demo.pojo.ElderlyServiceOk;
import com.example.demo.service.ElderlyBasicOkService;
import com.example.demo.service.ElderlyServiceOkService;
import com.example.demo.mapper.ElderlyServiceOkMapper;
import com.example.demo.service.ElderlyServiceService;
import com.example.demo.service.ServiceService;
import com.example.demo.vo.ElderlyServiceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangqi
 * @description 针对表【db_elderly_service_ok】的数据库操作Service实现
 * @createDate 2024-01-18 16:29:39
 */
@Service
public class ElderlyServiceOkServiceImpl extends ServiceImpl<ElderlyServiceOkMapper, ElderlyServiceOk>
        implements ElderlyServiceOkService {
    @Autowired
    private ElderlyServiceService elderlyServiceService;
    @Autowired
    private ElderlyBasicOkService elderlyBasicOkService;
    @Autowired
    private ServiceService serviceService;

    @Override
    @Transactional
    public void ok(OkDTO okDTO) {
        // A 具体的某个方案是否可行 灵活
        // B 某个老人的某个方案是否可行 操作简单
        // C A+B 可选方案多
        Long id = okDTO.getId();
        Boolean ok = okDTO.getOk();
        if (ok) {
            ElderlyService one = elderlyServiceService.lambdaQuery().eq(ElderlyService::getId, id).one();
            save(BeanUtil.copyProperties(one, ElderlyServiceOk.class));
        }
        elderlyServiceService.lambdaUpdate().eq(ElderlyService::getId, id).remove();
    }

    @Override
    @Transactional
    public List<ElderlyServiceVO> getElderlyService() {
        // TODO 这里先不分页查询了 之后想办法
        // 获取当前用户下的全部老人的id
        Long id = BaseContext.getCurrentId();
        List<ElderlyBasicOk> elderlyList = elderlyBasicOkService.lambdaQuery().eq(ElderlyBasicOk::getUserId, id).list();
        List<Long> idList = elderlyList.stream()
                .map(ElderlyBasicOk::getId)
                .toList();
        // 根据这些id来查找老人相关的服务
        List<ElderlyServiceVO> list = new ArrayList<>();

        for (Long l : idList) {
            List<ElderlyServiceOk> elderlyServices = lambdaQuery().eq(ElderlyServiceOk::getElderlyId, l).list();
            List<Long> serviceIds = elderlyServices.stream()
                    .map(ElderlyServiceOk::getServiceId)
                    .toList();
            List<com.example.demo.pojo.Service> services =
                    serviceService.lambdaQuery().in(com.example.demo.pojo.Service::getId, serviceIds).list();
            list.add(ElderlyServiceVO.builder().serviceList(services)
                    .elderlyId(l).build());
        }
        return list;
    }
}




